<?php
/**
 * Created by PhpStorm.
 * User: 85210755@qq.com
 * NickName: 柏宇娜
 * Date: 2019/5/31 13:52
 */
include_once './vendor/autoload.php';

use think\facade\Db;
use lib\Task;

//use GuzzleHttp\Client;
//use GuzzleHttp\Cookie\CookieJar;
//
//$c1 = [
//    'CNZZDATA5647345'             => 'cnzz_eid%3D956176011-1559531113-https%253A%252F%252Fwww.17k.com%252F%26ntime%3D1559531113',
//    'UM_distinctid'               => '16b1b7a96ec299-0ae4a69ef76aa7-39395704-1fa400-16b1b7a96edd7',
//    'accessToken'                 => 'id%3D71945827%26nickname%3D%25E6%259F%258F%25E5%25AE%2587%25E5%25A8%259C%26avatarUrl%3Dhttps%253A%252F%252Fcdn.static.17k.com%252Fuser%252Favatar%252F07%252F27%252F58%252F71945827.jpg%26e%3D1575086157%26s%3D5a6e1325f0dfec33',
//    'c_csc'                       => 'web',
//    'sensorsdata2015jssdkcross'   => '%7B%22distinct_id%22%3A%2271945827%22%2C%22%24device_id%22%3A%2216b1b7a96fc591-0c71ff3751bae3-39395704-2073600-16b1b7a96fd6ec%22%2C%22props%22%3A%7B%7D%2C%22first_id%22%3A%226281ad98-96b6-a023-1907-7c1c661cb2fe%22%7D',
//];
//
//$jar = CookieJar::fromArray($c1, 'www.17k.com');
//
//$client = new Client(['cookies' => $jar]);
//
//$response = $client->request('GET', 'https://www.17k.com/ck/book/1209865/chapter/22151205?subAllPrice=1&appKey=2406394919', ['verify' => false]);
//
//$arr = json_decode($response->getBody()->getContents(), true);
//
//var_dump($arr['data']['content'][0]['text']);
//
//exit;


/*---------------以上测试OK------------------*/

//$str = '第二章 道士篇捉妖记 （二）';
//var_dump(trim(substr($str, 0, stripos($str, ' '))));
//var_dump(trim(substr($str, stripos($str, ' '))));
//die;
//test redis...
//$con   = [
//    'host'    => '127.0.0.1',
//    'port'    => '6379',
//    'auth'    => '',
//    'dbindex' => 0
//];
//
//$redis = new \Redis();
//$redis->connect($con['host'], $con['port']);
//if ($con['auth']) {
//    $redis->auth($con['auth']);
//}
//var_dump($redis->ping());//+PONG
//die;

//检查代理
//proxy test...
//$res = stream_socket_client('61.48.52.125:8060', $errno, $errstr, 2);
//var_dump($errno);
//var_dump($errstr);
//var_dump($res);
//die;
date_default_timezone_set('Asia/Shanghai');
$db_conf    = [
    'default'     => 'mysql',
    'connections' => [
        'mysql' => [
            'type'     => 'mysql',
            'hostname' => '127.0.0.1',
            'username' => 'root',
            'password' => '123456',
            'database' => 'pickup',
            'prefix'   => 'fn_',
            'charset'  => 'utf8',
        ]
    ]
];
$redis_conf = [
    'host'    => '127.0.0.1',
    'port'    => '6379',
    'dbindex' => 0
];
$taskquery  = include_once './config.php';
//afterTask
Db::setConfig($db_conf);
//转换中文section_no
//$ids = Db::name('novel_section')->where('nid', 2)->column('sid');
//foreach ($ids as $index => $no) {
//    Db::execute("update fn_novel_section set section_no = CONVERT(section_chinese_no,UNSIGNED INTEGER) where sid={$no}");
//}
//die;

if (count($taskquery['resort'])) {
    foreach ($taskquery['resort'] as $sort_nid) {
        $list  = Db::name('novel_section')->where('nid', $sort_nid)->order('section_no ASC')->field('sid,section_no')->select();
        $count = count($list);
        foreach ($list as $index => $item) {
            $data = ['prev' => 0];
            if ($index > 0) {
                $data['prev'] = $list[$index - 1]['sid'];
            }
            if ($index + 1 == $count) {
                $data['next'] = 0;
            } else {
                $data['next'] = $list[$index + 1]['sid'];
            }
            Db::name('novel_section')->where('sid', $item['sid'])->update($data);
        }
    }
}


//测试redis
//$redis = new \Redis;
//$redis->connect($redis_conf['host'], $redis_conf['port']);
//
//$redis->set('test',123);
//var_dump($redis->get('testss'));
//$redis->del('test');
//exit();

$filter = [''];

$task = new Task($redis_conf);
$base = 'https://www.17k.com';
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);

//重试comic_chapter_images失败
//$task->aloneImg();
//die;
//$base_url = 'https://m.yymh666.com/';
//$ticket   = '3601FFdsGpbR2GwhsRrVstZiIKZJbfl5nSC3l5L2EgtYgVab6DNCu5cO6ktvPvPXo2LOM8D3A/l4GMAWwxWFAjLCbS7bE+1laBU';
//$task->getCategory($base_url, $ticket);
//$task->getComicChapter($base_url, $ticket);
//$path = '/comic/25/';
//echo $task->getComicCover('https://img.cb133.com/b1179/base/6d4f36b8-6f6b-4a75-97f2-8f75f6531e1d.jpg', $path).PHP_EOL;
//$ids = [];//^271
//for ($i = 291; $i < 372; $i++) {
//    array_push($ids, $i);
//}
//foreach ($ids as $id) {
//    $task->fixComicChaptrer($id);
//}
//exit();

//$task->content($base, 2688081, 452, 30, 0);
//exit;

//  https://www.17k.com/all/book/2_0_0_0_3_0_2_0_1.html

foreach ($taskquery['novel'] as $index => $item) {
    $id = $task->book($base, $item[0]);
    if ($id !== false) {
        $task->content($base, $item[0], $id, $item[1], $item[2]);
    }
}

